package com.example.demo.controller;

import com.example.demo.entity.User;
import com.example.demo.mapper.UserMapper;
import com.example.demo.utils.PagingResult;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

@CrossOrigin
@RestController
@RequestMapping("/user") //控制器的前缀地址
public class UserController {
    
    @Autowired
    private UserMapper userMapper;
    
    /**
     * 获取分页的用户数据
     * @param limit 每页数量
     * @param offset 偏移量
     * @param field 搜索字段
     * @param code 搜索关键词
     * @return 分页结果
     */
    @GetMapping("/list")
    public PagingResult<User> select(
            @RequestParam(required = false, name = "limit", defaultValue = "10") int limit,
            @RequestParam(required = false, name = "offset", defaultValue = "0") int offset,
            @RequestParam(required = false, name = "field") String field,
            @RequestParam(required = false, name = "code") String code
    ) {
        List<User> userList;
        int total;
        
        if (field != null && code != null && !code.trim().isEmpty()) {
            userList = userMapper.findByPageAndSearch(offset, limit, field, code);
            total = userMapper.countBySearch(field, code);
        } else {
            userList = userMapper.findByPage(offset, limit);
            total = userMapper.count();
        }
        
        return new PagingResult<User>(userList, total);
    }
    
    /**
     * 根据ID获取用户
     * @param user_no 用户ID
     * @return 用户对象
     */
    @GetMapping("/{user_no}")
    public User getById(@PathVariable("user_no") int user_no) {
        return userMapper.findById(user_no);
    }
    
    /**
     * 添加用户
     * @param user 用户对象
     * @return 影响行数
     */
    @PostMapping("/add")
    public int add(@RequestBody User user) {
        return userMapper.insert(user);
    }
    
    /**
     * 更新用户
     * @param user 用户对象
     * @return 影响行数
     */
    @PutMapping("/update")
    public int update(@RequestBody User user) {
        return userMapper.update(user);
    }
    
    /**
     * 删除用户
     * @param user_no 用户ID
     * @return 影响行数
     */
    @DeleteMapping("/delete/{user_no}")
    public int delete(@PathVariable("user_no") int user_no) {
        return userMapper.delete(user_no);
    }
    
    /**
     * 用户登录
     * @param name 用户名
     * @param password 密码
     * @return 登录结果
     */
    @PostMapping("/login")
    public Map<String, Object> login(
            @RequestParam("name") String name,
            @RequestParam("password") String password
    ) {
        Map<String, Object> result = new HashMap<>();
        
        try {
            // 调用mapper层的登录方法
            User user = userMapper.login(name, password);
            
            if (user != null) {
                // 登录成功
                result.put("code", 200);
                result.put("msg", "登录成功");
                result.put("data", user);
            } else {
                // 登录失败
                result.put("code", 400);
                result.put("msg", "用户名或密码错误");
            }
        } catch (Exception e) {
            // 系统异常
            result.put("code", 500);
            result.put("msg", "系统异常: " + e.getMessage());
        }
        
        return result;
    }
}
